<template>
  <div class="main">
    <header class="header">
      <strong>{{ username }}</strong>
      <el-button type="warning" @click="loginOut">退出登陆</el-button>
    </header>
    <nav class="nav">
      <el-tree :data="navData" default-expand-all node-key="id" accordion @node-click="handleNodeClick" ></el-tree>
    </nav>
    <div class="content">
      <router-view v-slot="{Component}">
        <keep-alive>
          <component :is="Component" v-if="currentRoute.meta.keepAlive"></component>
        </keep-alive>
        <component :is="Component"  v-if="!currentRoute.meta.keepAlive"></component>
      </router-view>
    </div>
  </div>
</template>
<script lang="ts" setup>
import { onMounted ,ref} from 'vue';
import {useRoute,useRouter} from 'vue-router'
let username=ref<string>("")
username.value=sessionStorage.getItem("username") as string
let ArrString=sessionStorage.getItem("menu")
let menuData=JSON.parse(ArrString||"")
menuData.sort((a,b)=>a.id-b.id)
let menu=menuData.map(item=>{
  return {
    id:item.id,
    path: `/system/${item.path}`,
    label:item.authName,
  }
})
const router=useRouter()
const currentRoute=useRoute()
interface Tree {
  id:string
  label: string
  path: string
  children?: Tree[]
}
menu.unshift({
  id:9999,
  path:'/index',
  label:'首页',
})
const navData:Tree[]=menu
const handleNodeClick=(t)=>{
  router.push(t.path)
}
const loginOut=()=>{
  sessionStorage.removeItem("token")
  sessionStorage.removeItem("refreshToken")
  router.push("/login")
}
const defaultProps = {
  children: 'children',
  label: 'label',
}
</script>
<style lang="less" scoped>
:deep(.is-current){
  background: #f8f8f8 !important;
}
.main {
  width: 100vw;
  height: 100vh;
  display: grid;
  grid-template-rows: 50px 1fr;
  grid-template-columns: 160px 1fr;
  grid-template-areas: "h h"
    "nav main";

  .header {
    width: 100%;
    height: 50px;
    background: rgb(232, 241, 250);
    grid-area: h;
    display: flex;
    box-sizing: border-box;
    padding: 0 30px;
    justify-content: space-between;
    align-items: center;
  }

  .nav {
    grid-area: nav;
    background: rgb(224, 241, 249);
  }

  .content {
    grid-area: main;
    overflow: auto;
    background: rgb(248, 248, 240);
  }
}
</style>